/**
 * author wen
 * &copy; CUNYU GROUP
 * email wjd_13@hotmail.com
 */
package com.cunyu.service;

import cn.tom.kit.StringUtil;
import com.cunyu.bean.Form;
import com.cunyu.bean.ReqData;
import com.cunyu.bean.RespData;
import com.cunyu.dao.DBQuery;
import com.cunyu.util.BaseUtil;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 返佣记录查询
 * @author win
 * 2019/05/28-09:50
 */
@Service
public class InvitePayService extends BaseService{

    //
    public RespData<List<Map<String, Object>>> view(ReqData reqData, Form form){
        String sql = "select * ,(select name from userinfo where uid = invitepay.uid) sname," +
                " (select tel from suser where id = uid) tel from invitePay where inviteId = ? " ;
        DBQuery query = new DBQuery(sql) ;
        query.setParams(Integer.valueOf(form.getId())) ;
        if(StringUtil.hasLength(form.getCtime())){
            Date s = BaseUtil.getDate(form.getCtime()) ;
            Date e = DateUtils.addDays(s,1) ;
            query.and("ctime >= ? ").setParams(s) ;
            query.and("ctime < ? ").setParams(e) ;
        }
        if(StringUtil.hasLength(form.getType())){
            query.and(" type = ? ").setParams(Integer.valueOf(form.getType())) ;
        }

        return page(query.getSql(),reqData, query.getParams()) ;
    }

    // 查询金额相关
    public Map<String, Object> count(Form form){
        String sql = "select a.uid,a.commission,a.money,a.score," +
                "(select count(s.*) from suser s where s.inviteid = a.uid) ins," +
                "(select sum(it.pay) from invitepay it where it.inviteid = a.uid) sums" +
                " from account a where a.uid =?" ;
        return getMap(sql,Integer.valueOf(form.getId()) ) ;
    }

    @Transactional(rollbackFor = Exception.class)
    public void withdraw(int uid, int money, String des) {
        String m = "select commission from account where uid = ? " ;
        int mI = getInt(m, uid);
        if(mI < money){
            logger.info(uid + " withdraw error commission "+mI +",want "+money) ;
            return;
        }
        String sqlIn = "insert into invitepay (inviteID,pay,type) values (?,?,?)" ;
        exec(sqlIn,uid,-money,1) ;
        String sqlC = "update account set commission = commission - ? where uid = ? " ;
        exec(sqlC,money,uid) ;
    }

    public RespData<List<Map<String, Object>>> getPage(ReqData reqData, Form form) {
        reqData.setSort("su.ctime");
        reqData.setOrder("desc");
        String sql = "select su.tel,ui.name,ac.money,ac.score,ac.commission,su.id " +
                "from suser su left join userinfo ui on su.id = ui.uid " +
                "left join account ac on ac.uid = su.id where 1= 1 " ;
        DBQuery query = new DBQuery(sql) ;
        if(StringUtil.hasLength(form.getTel())){
            query.and(" su.tel = ? ").setParams(Long.valueOf(form.getTel())) ;
        }
        if(StringUtil.hasLength(form.getName())){
            query.and(" ui.name like ? ").setParams("%"+form.getName()+"%");
        }
        return page(query.getSql(),reqData,query.getParams()) ;
    }
}
